﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace DomainModel.Entities
{
    // This is the Linq to SQL class for the Jobs table.
    // This is the bottom layer of the DAL.
    [Table (Name = "Jobs")]
    public class Job : IDataErrorInfo
    {
        [Column(Name = "jobID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int JobID { get; set; }

        [Column(Name = "job_title")] public string JobTitle { get; set; }
        [Column(Name = "description")] public string JobDescription { get; set; }
        [Column(Name = "salary")] public double Salary { get; set; }

        [Association(OtherKey = "JobID")] // Many to many relationship for questions <-> jobs.
        private EntitySet<QuestionnaireEntry> _questionnaireEntries = new EntitySet<QuestionnaireEntry>();
        public IList<QuestionnaireEntry> QuestionnaireEntries { get { return _questionnaireEntries.ToList().AsReadOnly(); } }

        [Association(OtherKey = "PositionID")] // Many to many relationship for questions <-> jobs.
        private EntitySet<Position> _positions = new EntitySet<Position>();
        public IList<Position> Positions { get { return _positions.ToList().AsReadOnly(); } }

        public string this[string fieldName]
        {
            get
            {
                if (((fieldName == "JobTitle") && string.IsNullOrEmpty(JobTitle)) || 
                    ((fieldName == "JobTitle") && (JobTitle.Length > 25)))
                    return "Job Title must not be empty or longers than 25 characters.";
                if (((fieldName == "JobDescription") && string.IsNullOrEmpty(JobDescription)) || 
                    ((fieldName == "JobDescription") && (JobTitle.Length > 8000)))
                    return "Job Description must not be empty or longer than 8000 characters.";
                if ((fieldName == "Salary") && Salary < 0)
                    return "Salary must be greater than or equal to 0.";

                return null;
            }
        }

        public string Error { get { return null; } }
    }
}
